﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using AOCRivaTunerReader;

namespace AOC19NET {
	/// <summary>
	/// Interaction logic for ConfigurationWindow.xaml
	/// </summary>
	public partial class ConfigurationWindow : Window {
		/*-------------------------------------------------------------------------*/
		/* MEMBERS																   */
		/*-------------------------------------------------------------------------*/
		#region MEMBERS
		/// <summary>
		/// Pointer to the main application.
		/// </summary>
		internal AOC19 m_MainApp;
		#endregion

		/*-------------------------------------------------------------------------*/
		/* METHODS																   */
		/*-------------------------------------------------------------------------*/
		#region METHODS
		/// <summary>
		/// Configuration Window for the AOC19NET applet.
		/// </summary>
		internal ConfigurationWindow(AOC19 _CallerApp) {
			InitializeComponent();

			// Keep a pointer to the caller
			m_MainApp = _CallerApp;

			// Load current settings
			LoadConfig();

			// Bind the Color property of the ColorPicker to our own property
			Binding ColorBinding = new Binding("Color");	// "Color" property
			ColorBinding.Source = m_ColorPicker;			// m_ColorPicker
			this.SetBinding(ConfigurationWindow.s_BaseColorProperty, ColorBinding);
		}

		/// <summary>
		/// Loads the current configuration. To be called each time the window is showed.
		/// </summary>
		public void LoadConfig() {
			// General tab
			m_ColorPicker.Color = Properties.Settings.Default.BaseColor;

			// GMail tab
			m_GMailUserTextBox.Text = Properties.Settings.Default.GMailUsername;
			m_GMailPasswordTextBox.Password = Properties.Settings.Default.GMailPassword;
			m_GMailRefreshTextBox.Text = Properties.Settings.Default.GMailUpdateFrequency.ToString();

			// RT tab
			AOC19NET.Controls.LabeledTextBox[] vRTGraphsLabels = { m_Graph0Label, m_Graph1Label, m_Graph2Label, m_Graph3Label, m_Graph4Label };
			AOC19NET.Controls.LabeledTextBox[] vRTGraphsMaxValues = { m_Graph0Max, m_Graph1Max, m_Graph2Max, m_Graph3Max, m_Graph4Max };
			ComboBox[] vRTGraphsSources = { m_Graph0Source, m_Graph1Source, m_Graph2Source, m_Graph3Source, m_Graph4Source };
			CRivaTunerPageSettings CurrentSettings = Properties.Settings.Default.RTPageSettings;
			CRivaTunerReader RTReader = new CRivaTunerReader();
			RTReader.Update();
			string[] vEntries = RTReader.GetEntriesList();

			// Fill each RT graph settings
			for(int i = 0; i < vRTGraphsLabels.Length; i++) {
				// Fill the label and max value
				vRTGraphsLabels[i].TextBox = CurrentSettings.m_vGraphLabels[i];
				vRTGraphsMaxValues[i].TextBox = CurrentSettings.m_vGraphMaxValues[i].ToString();

				// Fill the combo box
				vRTGraphsSources[i].Items.Clear();
				for(int j = 0; j < vEntries.Length; j++) {
					vRTGraphsSources[i].Items.Add(vEntries[j]);

					// If it's the current one, select it
					if(vEntries[j] == CurrentSettings.m_vRTKeys[i]) {
						vRTGraphsSources[i].SelectedItem = vEntries[j];
					}
				}

				// Disable combo box if RTHM is not running
				vRTGraphsSources[i].IsEnabled = (vEntries.Length != 0);
			}
			
			// The net graph is apart
			m_NetGraphMax.TextBox = CurrentSettings.m_vGraphMaxValues[5].ToString();
		}

		/// <summary>
		/// This is to avoid using the close button - that would cause the application to end.
		/// </summary>
		/// <param name="e">Event arguments.</param>
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
			base.OnClosing(e);
			e.Cancel = true;
		}

		/// <summary>
		/// Callback for when the Apply Button is clicked.
		/// </summary>
		/// <param name="sender">Event Sender.</param>
		/// <param name="e">Event Arguments.</param>
		private void m_ApplyButton_Click(object sender, RoutedEventArgs e) {
			// Save new settings
			// General Tab
			Properties.Settings.Default.BaseColor = m_ColorPicker.Color;

			// GMail Tab
			Properties.Settings.Default.GMailUsername = m_GMailUserTextBox.Text;
			Properties.Settings.Default.GMailPassword = m_GMailPasswordTextBox.Password;
			try {
				Properties.Settings.Default.GMailUpdateFrequency = Int32.Parse(m_GMailRefreshTextBox.Text);
			} catch {
				Properties.Settings.Default.GMailUpdateFrequency = 60;
			}

			// RT Tab
			AOC19NET.Controls.LabeledTextBox[] vRTGraphsLabels = { m_Graph0Label, m_Graph1Label, m_Graph2Label, m_Graph3Label, m_Graph4Label };
			AOC19NET.Controls.LabeledTextBox[] vRTGraphsMaxValues = { m_Graph0Max, m_Graph1Max, m_Graph2Max, m_Graph3Max, m_Graph4Max };
			ComboBox[] vRTGraphsSources = { m_Graph0Source, m_Graph1Source, m_Graph2Source, m_Graph3Source, m_Graph4Source };
			for(int i = 0; i < vRTGraphsLabels.Length; i++) {
				try {
					Properties.Settings.Default.RTPageSettings.m_vGraphLabels[i] = vRTGraphsLabels[i].m_TextBox.Text;
					Properties.Settings.Default.RTPageSettings.m_vGraphMaxValues[i] = float.Parse(vRTGraphsMaxValues[i].m_TextBox.Text);
					int iSelectedSource = vRTGraphsSources[i].SelectedIndex;
					if(iSelectedSource != -1) {
						Properties.Settings.Default.RTPageSettings.m_vRTKeys[i] = (string)vRTGraphsSources[i].Items[iSelectedSource];
					}
				} catch {
				}
			}
			Properties.Settings.Default.RTPageSettings.m_vGraphMaxValues[5] = float.Parse(m_NetGraphMax.m_TextBox.Text);
			
			// Save new settings
			Properties.Settings.Default.Save();

			// Apply new settings
			m_MainApp.ApplyColorScheme(Properties.Settings.Default.BaseColor);
			m_MainApp.m_RivaTunerPage.LoadSettings();

			// Hide window
			this.Visibility = Visibility.Hidden;
		}

		/// <summary>
		/// Callback for when the Cancel Button is clicked.
		/// </summary>
		/// <param name="sender">Event Sender.</param>
		/// <param name="e">Event Arguments.</param>
		private void m_CancelButton_Click(object sender, RoutedEventArgs e) {
			// Hide the window
			this.Visibility = Visibility.Hidden;

			// Restore color
			m_MainApp.ApplyColorScheme(Properties.Settings.Default.BaseColor);
		}

		/// <summary>
		/// When the password box gets the focus, select all.
		/// </summary>
		/// <param name="sender">Event Sender.</param>
		/// <param name="e">Event Arguments.</param>
		private void m_GMailPasswordTextBox_GotFocus(object sender, RoutedEventArgs e) {
			m_GMailPasswordTextBox.SelectAll();
		}
		#endregion

		/*-------------------------------------------------------------------------*/
		/* PROPERTIES															   */
		/*-------------------------------------------------------------------------*/
		#region BASE COLOR PROPERTY
		// We add this property to update in real time the color scheme.
		// The idea is to bind this property to the Color property in the ColorPick,
		// and then implement the OnValueChanged callback to update the scheme.

		/// <summary>
		/// "BaseColor" property accessor.
		/// </summary>
		public Color m_BaseColor {
			get {
				return (Color)GetValue(s_BaseColorProperty);
			}
			set {
				SetValue(s_BaseColorProperty, (Color)value);
			}
		}

		/// <summary>
		/// "BaseColor" property definition and registration.
		/// </summary>
		public static readonly DependencyProperty s_BaseColorProperty =
				DependencyProperty.Register(
				"BaseColor", typeof(Color), typeof(ConfigurationWindow),
				new PropertyMetadata(Colors.Aquamarine,
					new PropertyChangedCallback(BaseColorChangedCallback)
				)
			);

		/// <summary>
		/// "BaseColor" property change callback.
		/// </summary>
		/// <param name="_Sender">Event sender</param>
		/// <param name="_Arguments">Event arguments.</param>
		private static void BaseColorChangedCallback(DependencyObject _Sender, DependencyPropertyChangedEventArgs _Arguments) {
			ConfigurationWindow Sender = (ConfigurationWindow)_Sender;
			Sender.m_MainApp.ApplyColorScheme((Color)_Arguments.NewValue);
		}
		#endregion
	}
}
